<?php
/*
*用於後臺的菜單 By yan
*對應的顯示i18n是 menu
*/
namespace yan\backendclient\widgets;

use Yii;
use yii\helpers\Html;
use yii\helpers\Url;

use yan\backendclient\models\Menu;

class BackendMenuJson extends \yii\base\Widget
{
    public $controller_id;
    public $module_id;
    public $action_id;
    public $type_id;
    public $power;
    public function init(){
        $this->controller_id = Yii::$app->controller->id;
        
        $this->module_id = Yii::$app->controller->module->id;
        $this->module_id = $this->module_id == APP_NAME . '-backend' ? null : $this->module_id;
        
        $this->action_id = Yii::$app->controller->action->id;
        
        $this->type_id = Yii::$app->request->get('type');
    }
    public function run(){
        // 第一級目錄
        $menuList = $this->findMenus();
        $menuList = $this->buildMenuTree(0, $menuList, $this->power);
        return $menuList;
    }
    public function getMenus($power){
        $menuList = $this->findMenus();
        $menuList = $this->buildMenuTree(0, $menuList, $power);
        return $menuList;
    }
    // 獲取菜單數據
    public function findMenus(){
        return Menu::find()
            ->andWhere(['status'=>1])
            ->orderBy('sort_order asc')
            ->asArray()->all();
    }

    private function buildMenuTree($pid, $menuList, $power=null){
        $treeList = [];
        foreach ($menuList as $v) {
            if ($pid == $v['pid']) {
                $node = (array)$v;

                $flag_show = self::checkRoleShow($v, $power);
                $child = $this->buildMenuTree($v['id'], $menuList);
                if( $flag_show ){
                    $node = self::formatMenuData($v);
                    if (!empty($child)) {
                        $node['children'] = $child;
                    }else{
                        $node['type'] =1;
                    }
                    // todo 后续此处加上用户的权限判断
                    $treeList[] = $node;
                }elseif(!empty($child)){
                    $node = self::formatMenuData($v);
                    $node['children'] = $child;
                    $treeList[] = $node;
                }
                
            }
        }
        return $treeList;
    }

    public function formatMenuData($data){
        return [
            'id'=>'menu-'.$data['id'],
            'title' => !empty($data['i18n']) ? Yii::t('backend_menu', $data['i18n']) : $data['name'],
            'href' => self::formatFrontendUrl($data),
            "icon"=> "layui-icon layui-icon-auz",
            "target"=> "_self",
            "type"=> 0,
            "openType"=> "_component",
            "className"=>'item-'.$data['id'],
        ];
    }

    public static function urlToRole($url){
        $route = is_array($url) ? $url['0'] : $url;

        $route = explode('/', $route);

        if( empty($route['0']) ){
            unset($route['0']);
        }
        $str_route = implode('.', $route);
        return $str_route;
    }

    private function checkShow($url, $power=null){
        if( $power == 'admin' ) return true;
        $route = self::urlToRole($url);
        $flagRoute = Yii::$app->acl->isAllow($route, $power);
        return $flagRoute;
    }

    private function checkRoleShow($data, $power=null){
        if( $power == 'admin' ) return true;
        $route = self::getBackendUrlRole($data);
        $flagRoute = Yii::$app->acl->isAllow($route);
        return $flagRoute;
    }

    // 設置後臺連接
    public static function setBackendUrl($data){
        $r = !empty($data['module_id']) ? '/'. $data['module_id'] : '';
        $r .= !empty($data['controller_id']) ? '/'. $data['controller_id'] : 'default';
        $r .= !empty($data['action_id']) ? '/'. $data['action_id'] : 'index';
        return [$r, 'type'=>!empty($data['type']) ? $data['type'] : null];
    }
    public static function getBackendUrlRole($data){
        $r = !empty($data['module_id']) ? '/'. $data['module_id'] : '';
        $r .= !empty($data['controller_id']) ? '/'. $data['controller_id'] : 'default';
        $r .= !empty($data['action_id']) ? '/'. $data['action_id'] : 'index';
        return self::urlToRole([$r]);
    }
    public static function formatFrontendUrl($data){
        $url = self::setBackendUrl($data);
        if(gettype($url) == 'string'){
            return $url;
        }
        return \yii\helpers\Url::to($url);
    }
}
